Skip to main content

Combination Sum III

Question

None

Example 1
Input: k = 3, n = 7

Output: [[1,2,4]]

Solution

all//Combination Sum III.py


from itertools import combinations

def combinationSum3(k, n):
# Generate all possible combinations of k numbers that add up to n
combinations_list = combinations(range(1, 10), k)
valid_combos = []
for combo in combinations_list:
if sum(combo) == n:
valid_combos.append(combo)
return valid_combos